<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use DB;
use Session;

class FansController extends Controller
{
        // 加关注
	public function addFollow( Request $request )
	{
		if( empty(Session::get('user_id')) )
		{
			$this->show_msg( '请先登录' );
		}
		
		// 包含 要关注的用户id
		$data = $request -> except("_token");
		$follow_history = DB::table('fans')
				-> where( 'uid',Session::get('user_id') )
				-> where( 'follow_id',$data['follow_id'] )
				-> first();
		
		if( !empty($follow_history) )
		{
			// 找到关注 记录
			$this->show_msg( '您已关注' );
		}
				
		$insert = array();
		$insert['follow_id']=$data['follow_id'];
		$insert['uid']=Session::get('user_id');
        $insert['create_at']=date('Y-m-d H:i:s');
		
		// 写进粉丝表
		$insert_id = DB::table('fans')
					->insertGetId( $insert );
		if( $insert_id )
		{
			$this->show_msg( '关注成功' );
		}else
		{
			$this->show_msg( '关注失败' );
		}
		
	}

    //粉丝加关注
    public function geFollow( Request $request ){
        $data = $request -> except("_token");
        $id = DB::table('fans')
            -> where( 'uid',Session::get('user_id') )
            -> where( 'follow_id',$data['follow_id'] )
            -> first();
//        print_r($id);die;
//        $users = DB::table('fans')->where('follow_id',$id)->get();
//        print_r($users);die;
            $insert = array();
            $insert['follow_id']=$data['follow_id'];
            $insert['uid']=Session::get('user_id');
            $insert['create_at']=date('Y-m-d H:i:s');
            // 入库 粉丝表
            $insert_id = DB::table('fans')
                ->insertGetId( $insert );
            if( $insert_id )
            {
                $ids = DB::table('fans')
                    -> where( 'uid',$insert['follow_id'] )
                    -> where( 'follow_id',$insert['uid'] )
                    -> update($arr);
                if($ids){
                    $this->show_msg( '关注成功' );
                }
            }else
            {
                $this->show_msg( '关注失败' );
            }
    }

	
	//li 取消关注
    public function quFollow(Request $request){
        $data = $request -> except("_token");
        $follow_history = DB::table('fans')
            -> where( 'uid',Session::get('user_id') )
            -> where( 'follow_id',$data['follow_id'] )
            -> first();

        if( !empty($follow_history) )
        {
            // 找到关注 记录
            // 删除
            DB::table('fans')
                -> where( 'id',$follow_history->id )
                -> delete();
        }
        $this->show_msg( '取消关注成功' );
    }


	// 取消关注
	public function cancleFollow( Request $request )
	{
		if( empty(Session::get('user_id')) )
		{
			return redirect('/');
		}
		// 包含要取消关注的用户id
		$data = $request -> except("_token");
		$follow_history = DB::table('fans')
				-> where( 'uid',Session::get('user_id') )
				-> where( 'follow_id',$data['follow_id'] )
				-> first();
				
		if( !empty($follow_history) )
		{
			// 找到关注 记录
			// 删除
			DB::table('fans')
				-> where( 'id',$follow_history->id )
				-> delete();
		}
		$this->show_msg( '取消关注成功' );
		
	}
	
	
}
